package com.hulk.dryad.manage.framework.storage;


import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.amazonaws.services.s3.model.S3Object;
import com.hulk.dryad.common.exception.FileUploadException;

import com.hulk.dryad.manage.framework.s3.config.S3FsProperties;
import com.hulk.dryad.manage.framework.s3.template.FsTemplate;
import com.hulk.dryad.manage.framework.storage.config.StorageProperties;

import jodd.util.StringPool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.stream.Stream;


/**
 * @author hulk
 * @date 2020/7/16 16:10
 * @decrpt 云对象存储服务
 */
@Slf4j
@RequiredArgsConstructor
public class CloudStorage implements Storage {

	private final StorageProperties storageProperties;
	private final FsTemplate fsTemplate;

	@Override
	public void store(InputStream inputStream, String contextType, String fileName, String directory,String bucket) {
		S3FsProperties prop = fsTemplate.getS3FsProperties();
		if (StrUtil.isBlank(bucket)) {
			bucket = prop.getBucket();
		}
		try {
			fsTemplate.putObject(bucket, fileName, directory, inputStream,contextType);
		} catch (Exception e) {
			log.error("上传失败", e);
			throw new FileUploadException("上传失败",e);
		}

	}
	@Override
	public Stream<Path> loadAll(String directory,String bucket) {
		return null;
	}

	@Override
	public Path loadPath(String fileName, String directory, String bucket) {
		return null;
	}

	@Override
	public Resource loadAsResource(String fileName, String directory,String bucket) {
		S3FsProperties prop = fsTemplate.getS3FsProperties();
		if (StrUtil.isBlank(bucket)) {
			bucket = prop.getBucket();
		}
		try {
			String filePathName = fsTemplate.getObjectURL( bucket, directory, fileName,  7);
			URL url = new URL(filePathName);
			Resource resource = new UrlResource(url);
			if (resource.exists() || resource.isReadable()) {
				return resource;
			} else {
				return null;
			}
		} catch (MalformedURLException e) {
			log.error(e.getMessage(), e);
			return null;
		}
	}

	@Override
	public InputStream loadAsInputStream(String fileName, String directory, String bucket) {
		S3FsProperties prop = fsTemplate.getS3FsProperties();
		if (StrUtil.isBlank(bucket)) {
			bucket = prop.getBucket();
		}
		S3Object  s3Object  = fsTemplate.getObject(bucket,directory, fileName);
		if(s3Object==null){
			return null;
		}
		return s3Object.getObjectContent();
	}

	@Override
	public void delete(String fileName, String directory,String bucket) {
		S3FsProperties prop = fsTemplate.getS3FsProperties();
		if (StrUtil.isBlank(bucket)) {
			bucket = prop.getBucket();
		}
		fsTemplate.removeObject(bucket,directory,fileName);
	}
	@Override
	public String generateUri(String fileName, String directory, String bucket) {
		S3FsProperties prop = fsTemplate.getS3FsProperties();
		if (StrUtil.isBlank(bucket)) {
			bucket = prop.getBucket();
		}
	    String urlStr  = fsTemplate.getObjectURL( bucket, directory, fileName,  7);
		URL url = URLUtil.url(urlStr);
		String path = url.getPath();
		String query = url.getQuery();
		return path + StringPool.QUESTION_MARK+query;
	}

	@Override
	public String getHost(String fileName, String directory, String bucket) {
		S3FsProperties prop = fsTemplate.getS3FsProperties();
		if (StrUtil.isBlank(bucket)) {
			bucket = prop.getBucket();
		}
		String urlStr  = fsTemplate.getObjectURL( bucket, directory, fileName,  7);
		URL url = URLUtil.url(urlStr);
		String host = URLUtil.getHost(URLUtil.url(urlStr)).toString();
		int port = url.getPort();
		if(port < 0){
			return host;
		}
		return host+StrUtil.COLON+port;
	}
	@Override
	public  StorageProperties getProp() {
		return storageProperties;
	}


}
